package session.stateless;

import entity.ActivityLocalizedEntity;
import entity.AttractionEntity;
import entity.DailyItineraryEntity;
import entity.ItinerarySlotEntity;
import entity.TouristAccountEntity;
import entity.TouristEntity;
import entity.TripItineraryEntity;
import exception.ExistException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.ejb.Remove;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import util.entityhelper.DailyItineraryState;
import util.entityhelper.ItinerarySlotState;
import util.entityhelper.TripItineraryState;
import util.enumeration.ActivityType;
import util.enumeration.SingaporeArea;
import util.enumhelper.LocalizedActivityType;

/**
 *
 * @author Swee Zhi
 */
@Stateless
@LocalBean
public class ItinerarySessionBean {

    @PersistenceContext
    EntityManager em;
    private String selectedLocale;
    private TouristAccountEntity touristAccount;
    private TouristEntity tourist;
    private TripItineraryEntity tripItinerary;
    private DailyItineraryEntity dailyItinerary;
    private ItinerarySlotEntity itinerarySlot;
    // EXCEPTION
    private SystemMessagesBean systemMessages;

    /** Creates a new instance of ItinerarySessionBean */
    public ItinerarySessionBean() {
        systemMessages = new SystemMessagesBean();
    }

    public void setSelectedLocale(String locale) {
        this.selectedLocale = locale;
    }

    /* TRIP ITINERARY */
    // trip + its days are created here
    public Long createTripItinerary(Long touristAccountId, String itineraryName,
            Calendar arrivalDateTime, Calendar departureDateTime,
            String timeZone, String accomodationPostalCode,
            int noOfAdults, int noOfChildren)
            throws ExistException {

        // find tourist account
        touristAccount = em.find(TouristAccountEntity.class, touristAccountId);

        if (touristAccount == null) {
            throw new ExistException(systemMessages.getSystemMessage("TOURIST_ACCT_DOES_NOT_EXIST", selectedLocale));
        } else {
            // create new trip itinerary
            tripItinerary = new TripItineraryEntity();

            Date arrivalDate = arrivalDateTime.getTime();
            Time arrivalTime = new Time(arrivalDateTime.getTimeInMillis());
            TimeZone arrivalTimeZone = arrivalDateTime.getTimeZone();

            Date departureDate = departureDateTime.getTime();
            Time departureTime = new Time(departureDateTime.getTimeInMillis());
            TimeZone departureTimeZone = departureDateTime.getTimeZone();

            System.out.println("arrivalDateTime:" + arrivalDateTime.get(Calendar.YEAR));
            System.out.println("arrivalDateTime:" + arrivalDateTime.get(Calendar.MONTH));
            System.out.println("arrivalDateTime:" + arrivalDateTime.get(Calendar.DAY_OF_MONTH));
            System.out.println("arrivalDateTime:" + arrivalDateTime.get(Calendar.HOUR_OF_DAY));

            System.out.println("departureDateTime:" + departureDateTime.get(Calendar.YEAR));
            System.out.println("departureDateTime:" + departureDateTime.get(Calendar.MONTH));
            System.out.println("departureDateTime:" + departureDateTime.get(Calendar.DAY_OF_MONTH));
            System.out.println("departureDateTime:" + departureDateTime.get(Calendar.HOUR_OF_DAY));

            // calculate no. of trip days
            long millisecondsArrival = arrivalDateTime.getTimeInMillis();
            long millisecondsDeparture = departureDateTime.getTimeInMillis();
            long diff = millisecondsDeparture - millisecondsArrival;
            long diffDays = diff / (24 * 60 * 60 * 1000);
            System.out.println("diffdays: " + diffDays);

            // create daily itineraries based on no. of trip days
            List<DailyItineraryEntity> dailyItineraries = new ArrayList<DailyItineraryEntity>();
            for (int i = 0; i < diffDays; i++) {
                Date nextDay = calculateNextDayByTravelPeriodAndDayNo(arrivalDateTime, i);
                DailyItineraryEntity newDailyItinerary = new DailyItineraryEntity();
                newDailyItinerary.setDayNo(i + 1);
                newDailyItinerary.setDayDate(nextDay);
                // link back trip itinerary for bi-directional rls
                newDailyItinerary.setTripItinerary(tripItinerary);
                dailyItineraries.add(newDailyItinerary);
            }

            tripItinerary.create(itineraryName, arrivalDate, arrivalTime, arrivalTimeZone, departureDate, departureTime, departureTimeZone, accomodationPostalCode, noOfAdults, noOfChildren);
            tripItinerary.setDailyItineraries(dailyItineraries);
            touristAccount.getTourist().getTripItineraries().add(tripItinerary);

            em.persist(touristAccount);
            em.flush();
        }
        
        System.out.println("tripItinerary ID backend: " + tripItinerary.getId());
        return tripItinerary.getId();
    }

    private Date calculateNextDayByTravelPeriodAndDayNo(Calendar arrivalDateTime, Integer dayNo) {
        int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;

        Date dayDate = new Date();
        dayDate.setTime(arrivalDateTime.getTimeInMillis() + (MILLIS_IN_DAY * dayNo));

        System.out.println("dayDate for day " + dayNo + ": " + dayDate);
        return dayDate;
    }

    public void addItineraryItems(Long dailyItineraryId, List<ItinerarySlotEntity> itineraryItems)
            throws ExistException {

        // find daily itinerary
        dailyItinerary = em.find(DailyItineraryEntity.class, dailyItineraryId);

        // trip itinerary for tourist does not exist;
        if (dailyItinerary == null) {
            // EXCEPTION
            throw new ExistException(systemMessages.getSystemMessage("DAILY_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        } else {
            for (ItinerarySlotEntity s : itineraryItems) {
                s.setDailyItinerary(dailyItinerary);
            }
            // add itinerary slots to daily itinerary
            dailyItinerary.setItinerarySlots(itineraryItems);

            em.persist(dailyItinerary);
            em.flush();
        }
    }
    
    public void updateItineraryItems(Long dailyItineraryId, List<ItinerarySlotEntity> itineraryItems)
            throws ExistException {

        // find daily itinerary
        dailyItinerary = em.find(DailyItineraryEntity.class, dailyItineraryId);
        System.out.println("0 clear: " + dailyItinerary.getItinerarySlots().size());

        // trip itinerary for tourist does not exist;
        if (dailyItinerary == null) {
            // EXCEPTION
            throw new ExistException(systemMessages.getSystemMessage("DAILY_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        } else {
            
//            Query q = em.createQuery("DELETE FROM ItinerarySlot t WHERE t.dailyItinerary.id = :dailyId");
//            q.setParameter("dailyId", dailyItineraryId);
            
            // remove itinerary slots from daily itinerary
            dailyItinerary.getItinerarySlots().clear();
            System.out.println("1 clear: " + dailyItinerary.getItinerarySlots().size());
            
            
            em.persist(dailyItinerary);
            em.flush();
            
            // find daily itinerary
            dailyItinerary = em.find(DailyItineraryEntity.class, dailyItineraryId);
            
            System.out.println("2 clear: " + dailyItinerary.getItinerarySlots().size());
            
            for (ItinerarySlotEntity d : itineraryItems) {
                System.out.println("itineraryItems: " + d.getActivityName());
            }
            
            System.out.println("Current daily ITI ID: " + dailyItinerary.getId());
            
            for (ItinerarySlotEntity s : itineraryItems) {
                s.setDailyItinerary(dailyItinerary);
                System.out.println("AFTER set: get from slot: " + s.getDailyItinerary().getId());
            }
            
            dailyItinerary.setItinerarySlots(itineraryItems);
            System.out.println("3 clear: " + dailyItinerary.getItinerarySlots().size());
            em.persist(dailyItinerary);
            em.flush();
        }
    }

//    public void addItineraryItem(Long dailyItineraryId,
//                                 String entityName, Long itemKey,
//                                 Calendar startDateTime, Calendar endDateTime)
//           throws ExistException {
//        
////        // find tourist account
////        tourist = em.find(TouristEntity.class, touristId);
////        
////        // tourist account for tourist does not exist;
////        if (tourist == null) {
////            // EXCEPTION
////            throw new ExistException(systemMessages.getSystemMessage("TOURIST_ACCT_DOES_NOT_EXIST", selectedLocale));
////        }
////        else {
////            // find trip itinerary
////            tripItinerary = em.find(TripItineraryEntity.class, tripItineraryId);
////            
////            // trip itinerary for tourist does not exist;
////            if (tripItinerary == null) {
////                // EXCEPTION
////                throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
////            }
////            else {
//                // find daily itinerary
//                dailyItinerary = em.find(DailyItineraryEntity.class, dailyItineraryId);
//                
//                // trip itinerary for tourist does not exist;
//                if (dailyItinerary == null) {
//                    // EXCEPTION
//                    throw new ExistException(systemMessages.getSystemMessage("DAILY_ITINERARY_DOES_NOT_EXIST", selectedLocale));
//                }
//                else {
//                    // create a new slot item
//                    ItinerarySlotEntity newItinerarySlot = new ItinerarySlotEntity();
//                    newItinerarySlot.setEntityName(entityName);
//                    newItinerarySlot.setItemKey(itemKey);
//                    newItinerarySlot.setStartDateTime(startDateTime);
//                    newItinerarySlot.setEndDateTime(endDateTime);
//                    
//                    // add to itinerary slots list
//                    dailyItinerary.getItinerarySlots().add(newItinerarySlot);
//                
//                    em.persist(dailyItinerary);
//                    em.flush();
//                }
////            }
////        }
//    }
    public void removeItineraryItem(Long dailyItineraryId, Long itinerarySlotId)
            throws ExistException {

        // find daily itinerary
        dailyItinerary = em.find(DailyItineraryEntity.class, dailyItineraryId);

        // trip itinerary for tourist does not exist;
        if (dailyItinerary == null) {
            // EXCEPTION
            throw new ExistException(systemMessages.getSystemMessage("DAILY_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        } else {
            for (ItinerarySlotEntity s : dailyItinerary.getItinerarySlots()) {
                if (s.getId().compareTo(itinerarySlotId) == 0) {
                    dailyItinerary.getItinerarySlots().remove(s);
                }
            }

            em.persist(dailyItinerary);
            em.flush();
        }
    }

    public List<TripItineraryState> getTripItinerariesReturnByTripItineraryState() throws ExistException {
        Query q = em.createQuery("SELECT t FROM TripItinerary t");
        List stateList = new ArrayList();

        if (q.getResultList().isEmpty()) {
            throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        } else {
            int count = 0;
            for (Object o : q.getResultList()) {
                TripItineraryEntity t = (TripItineraryEntity) o;
                TripItineraryState itineraryState = new TripItineraryState(
                        t.getId(),
                        t.getItineraryName(),
                        t.getArrivalDate(),
                        t.getArrivalTime(),
                        t.getArrivalTimeZone(),
                        t.getDepartureDate(),
                        t.getDepartureTime(),
                        t.getDepartureTimeZone(),
                        t.getAccomodationPostalCode(),
                        t.getNoOfAdults(),
                        t.getNoOfChildren(),
                        t.getDailyItineraries());

                itineraryState.setTripNo(count++);

                // format date fields for display in data table / data list
                Locale userLocale = new Locale(selectedLocale);

                Calendar calArrivalDate = new GregorianCalendar();
                calArrivalDate.setTime(t.getArrivalDate());
                String formattedArrivalDateOnly =
                        calArrivalDate.get(Calendar.DAY_OF_MONTH) + " "
                        + calArrivalDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, userLocale) + " "
                        + calArrivalDate.get(Calendar.YEAR);
                String formattedArrivalTimeOnly =
                        t.getArrivalTime().toString() + "\n" + "(" + t.getArrivalTimeZone().getDisplayName(userLocale).toString() + ")";
//                String formattedArrivalTimeZoneOnly = 


                Calendar calDepartureDate = new GregorianCalendar();
                calDepartureDate.setTime(t.getDepartureDate());
                String formattedDepartureDateOnly =
                        calDepartureDate.get(Calendar.DAY_OF_MONTH) + " "
                        + calDepartureDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, userLocale) + " "
                        + calDepartureDate.get(Calendar.YEAR);
                String formattedDepartureTimeOnly =
                        t.getDepartureTime().toString() + "\n" + "(" + t.getDepartureTimeZone().getDisplayName(userLocale).toString() + ")";
//                String formattedDepartureTimeZoneOnly = 



                itineraryState.setFormattedDateTimeFields(formattedArrivalDateOnly, formattedArrivalTimeOnly, formattedDepartureDateOnly, formattedDepartureTimeOnly);
                stateList.add(itineraryState);
            }
            count = 0;
        }
        return stateList;
    }

    public List<DailyItineraryState> getDailyItinerariesByTripItineraryIDReturnByDailyItineraryState(Long tripItineraryId) throws ExistException {
        Query q = em.createQuery("SELECT d FROM DailyItinerary d WHERE d.tripItinerary.id = :tripId");
        q.setParameter("tripId", tripItineraryId);
        List stateList = new ArrayList();

        if (q.getResultList().isEmpty()) {
            throw new ExistException(systemMessages.getSystemMessage("DAILY_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        } else {
            for (Object o : q.getResultList()) {
                DailyItineraryEntity t = (DailyItineraryEntity) o;
                DailyItineraryState itineraryState = new DailyItineraryState(
                        t.getId(),
                        t.getDayNo(),
                        t.getDayDate(),
                        t.getItinerarySlots());

//                // format date fields for display in data table / data list
//                Locale userLocale = new Locale(selectedLocale);
//                
//                Calendar calArrivalDate = new GregorianCalendar();
//                calArrivalDate.setTime(t.getArrivalDate());
//                String formattedArrivalDateOnly = 
//                        calArrivalDate.get(Calendar.DAY_OF_MONTH) + " " +
//                        calArrivalDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, userLocale) + " " +
//                        calArrivalDate.get(Calendar.YEAR);
//                String formattedArrivalTimeOnly = 
//                        t.getArrivalTime().toString() + "\n" + "(" +  t.getArrivalTimeZone().getDisplayName(userLocale).toString() + ")";
////                String formattedArrivalTimeZoneOnly = 
//                        
//                
//                Calendar calDepartureDate = new GregorianCalendar();
//                calDepartureDate.setTime(t.getDepartureDate());
//                String formattedDepartureDateOnly = 
//                        calDepartureDate.get(Calendar.DAY_OF_MONTH) + " " +
//                        calDepartureDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, userLocale) + " " +
//                        calDepartureDate.get(Calendar.YEAR);
//                String formattedDepartureTimeOnly = 
//                        t.getDepartureTime().toString() + "\n" + "(" + t.getDepartureTimeZone().getDisplayName(userLocale).toString() + ")";
////                String formattedDepartureTimeZoneOnly = 
//                        
//                
//                
//                itineraryState.setFormattedDateTimeFields(formattedArrivalDateOnly, formattedArrivalTimeOnly, formattedDepartureDateOnly, formattedDepartureTimeOnly);
                stateList.add(itineraryState);
            }
        }
        return stateList;
    }

//    public List<TripItineraryEntity> getTripItinerariesReturnByTripItineraryEntity() throws ExistException {
//        Query q = em.createQuery("SELECT t FROM TripItinerary t");
//
//        if (q.getResultList().isEmpty()) {
//            throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
//        }
//        else {
//            return q.getResultList();
//        }
//    }
    public TripItineraryState getTripItineraryByIDReturnByTripItineraryState(Long tripItineraryId) throws ExistException {
        TripItineraryState tripItineraryState = null;

        // find account id
        Query q = em.createQuery("SELECT t FROM TripItinerary t WHERE t.id = :tripId");
        q.setParameter("tripId", tripItineraryId);

        if (q.getResultList().isEmpty()) {
            // show exception message using SystemMessage bean
            throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        } else {
            TripItineraryEntity t = (TripItineraryEntity) q.getSingleResult();
            tripItineraryState = new TripItineraryState(
                    t.getId(),
                    t.getItineraryName(),
                    t.getArrivalDate(),
                    t.getArrivalTime(),
                    t.getArrivalTimeZone(),
                    t.getDepartureDate(),
                    t.getDepartureTime(),
                    t.getDepartureTimeZone(),
                    t.getAccomodationPostalCode(),
                    t.getNoOfAdults(),
                    t.getNoOfChildren(),
                    t.getDailyItineraries());

            // format date fields for display in data table / data list
            Locale userLocale = new Locale(selectedLocale);

            Calendar calArrivalDate = new GregorianCalendar();
            calArrivalDate.setTime(t.getArrivalDate());
            String formattedArrivalDateOnly =
                    calArrivalDate.get(Calendar.DAY_OF_MONTH) + " "
                    + calArrivalDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, userLocale) + " "
                    + calArrivalDate.get(Calendar.YEAR);
            String formattedArrivalTimeOnly =
                    t.getArrivalTime().toString() + "\n" + "(" + t.getArrivalTimeZone().getDisplayName(userLocale).toString() + ")";
//                String formattedArrivalTimeZoneOnly = 


            Calendar calDepartureDate = new GregorianCalendar();
            calDepartureDate.setTime(t.getDepartureDate());
            String formattedDepartureDateOnly =
                    calDepartureDate.get(Calendar.DAY_OF_MONTH) + " "
                    + calDepartureDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, userLocale) + " "
                    + calDepartureDate.get(Calendar.YEAR);
            String formattedDepartureTimeOnly =
                    t.getDepartureTime().toString() + "\n" + "(" + t.getDepartureTimeZone().getDisplayName(userLocale).toString() + ")";
//                String formattedDepartureTimeZoneOnly = 



            tripItineraryState.setFormattedDateTimeFields(formattedArrivalDateOnly, formattedArrivalTimeOnly, formattedDepartureDateOnly, formattedDepartureTimeOnly);
        }
        return tripItineraryState;
    }

    public DailyItineraryState getDailyItineraryByIDReturnByDailyItineraryState(Long dailyItineraryId) throws ExistException {
        DailyItineraryState dailyItineraryState = null;

        // find account id
        Query q = em.createQuery("SELECT d FROM DailyItinerary d WHERE d.id = :dailyId");
        q.setParameter("dailyId", dailyItineraryId);

        if (q.getResultList().isEmpty()) {
            // show exception message using SystemMessage bean
            throw new ExistException(systemMessages.getSystemMessage("DAILY_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        } else {
            DailyItineraryEntity t = (DailyItineraryEntity) q.getSingleResult();
            dailyItineraryState = new DailyItineraryState(
                    t.getId(),
                    t.getDayNo(),
                    t.getDayDate(),
                    t.getItinerarySlots());

            dailyItineraryState.setItinerarySlots(t.getItinerarySlots());
            // format date fields for display in data table / data list
//                Locale userLocale = new Locale(selectedLocale);
//                
//                Calendar calDayDate = new GregorianCalendar();
//                calDayDate.setTime(t.getDayDate());
//                String formattedDayDateOnly = 
//                        calDayDate.get(Calendar.DAY_OF_MONTH) + " " +
//                        calDayDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, userLocale) + " " +
//                        calDayDate.get(Calendar.YEAR);
//                String formattedArrivalTimeZoneOnly = 
//
//                dailyItineraryState.setFormattedDateTimeFields(formattedArrivalDateOnly, formattedArrivalTimeOnly, formattedDepartureDateOnly, formattedDepartureTimeOnly);
        }
        return dailyItineraryState;
    }

    public List<ItinerarySlotState> getItinerarySlotsByDailyItineraryIDReturnByItinerarySlotState(Long dailyItineraryId) throws ExistException {
        em.flush();
        Query q = em.createQuery("SELECT s FROM ItinerarySlot s WHERE s.dailyItinerary.id = :dailyId");
        q.setParameter("dailyId", dailyItineraryId);
        List stateList = new ArrayList();

        if (q.getResultList().isEmpty()) {
            throw new ExistException(systemMessages.getSystemMessage("ITINERARY_ITEMS_DO_NOT_EXIST", selectedLocale));
        } else {
            for (Object o : q.getResultList()) {
                System.out.println("size of list: " + q.getResultList().size());
                ItinerarySlotEntity t = (ItinerarySlotEntity) o;
                ItinerarySlotState itineraryState = new ItinerarySlotState(
                        t.getId(),
                        t.getEntityName(),
                        t.getdType(),
                        t.getItemKey(),
                        t.getStartTime(),
                        t.getEndTime(),
                        t.getActivityName(),
                        t.getActivityType(),
                        t.getTransportType());
                        
//                itineraryState.setLocalizedActivityInformation(selectedLocale, t.getActivityType(), t.getActivityName(), t.get);
//                itineraryState.setLocalizedActivityType(LocalizedActivityType.getLocalizedActivityTypeAsString(t.getActivityType(), selectedLocale));
//                itineraryState.setLocalizedActivityName(t.getActivityName());
              
                
                System.out.println("t.getEntityName(): " + t.getEntityName());
                System.out.println("t.itemKey: " + t.getItemKey());
                System.out.println("t.getdType(): " + t.getdType());

                if (t.getdType().equalsIgnoreCase("attraction")) {
                    System.out.println("in Attraction");
                    
                    Query qA = em.createQuery("SELECT a FROM " + t.getEntityName() + " a WHERE a.id = :activityId");
                    qA.setParameter("activityId", t.getItemKey());
                    
                    if (qA.getResultList().isEmpty()) {
                        // show exception message using SystemMessage bean
                        throw new ExistException(systemMessages.getSystemMessage("ATTRACTION_DOES_NOT_EXIST", selectedLocale));
                    } 
                    else {
                        AttractionEntity attraction = (AttractionEntity) qA.getSingleResult();
                        
//                        System.out.println("attraction.getLocale(): " + attraction.getLocale());
//                        System.out.println("selectedLocale: " + selectedLocale);
                        if (attraction.getLocale().equalsIgnoreCase("en_US")) {
                            System.out.println("selectedLocale: US: " + selectedLocale);
                            itineraryState.setLocalizedActivityInformation(selectedLocale, 
                                                                           attraction.getActivityType(), 
                                                                           attraction.getActivityName(),
                                                                           attraction.getSingaporeArea());
//                            System.out.println("itineraryState.getLocalizedActivityName() : " + itineraryState.getLocalizedActivityName());
//                            System.out.println("itineraryState.getLocalizedActivityType() : " + itineraryState.getLocalizedActivityType());
//                            System.out.println("itineraryState.getLocalizedSingaporeArea() : " + itineraryState.getLocalizedSingaporeArea());
                        }
                        else {
                            for (ActivityLocalizedEntity a : attraction.getLocalizedActivity()) {
                                if (a.getLocale().equalsIgnoreCase(selectedLocale)) {
//                                    System.out.println("selectedLocale other:: " + selectedLocale);
                                    itineraryState.setLocalizedActivityInformation(selectedLocale, 
                                                                                   attraction.getActivityType(), 
                                                                                   a.getLocalizedActivityName(),
                                                                                   attraction.getSingaporeArea());

//                                    System.out.println("itineraryState.getLocalizedActivityName() : " + itineraryState.getLocalizedActivityName());
//                                    System.out.println("itineraryState.getLocalizedActivityType() : " + itineraryState.getLocalizedActivityType());
//                                    System.out.println("itineraryState.getLocalizedSingaporeArea() : " + itineraryState.getLocalizedSingaporeArea());
                                }

                            }
                        }
                    }
                    
                }
                stateList.add(itineraryState);
            }
        }
        return stateList;
    }

//    public List<ItinerarySlotEntity> getItinerarySlotsReturnByItinerarySlotEntity() throws ExistException {
//        Query q = em.createQuery("SELECT s FROM ItinerarySlot s");
//
//        if (q.getResultList().isEmpty()) {
//            throw new ExistException(systemMessages.getSystemMessage("ITINERARY_ITEMS_DO_NOT_EXIST", selectedLocale));
//        }
//        else {
//            return q.getResultList();
//        }
//    }
    public void updateTripItineraryName(Long tripItineraryId, String itineraryName) throws ExistException {
        // find tripItinerary
        tripItinerary = em.find(TripItineraryEntity.class, tripItineraryId);
        
        // tripItinerary for tourist does not exist;
        if (tripItinerary == null) {
            // EXCEPTION
            throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        }
        else {
            tripItinerary.setItineraryName(itineraryName);
            
            em.persist(tripItinerary);
            em.flush();
        }
    }
    
//    public void updateTripTravelPeriod(Long tripItineraryId, Calendar arrivalDateTime, Calendar departureDateTime) throws ExistException {
//        // find tripItinerary
//        tripItinerary = em.find(TripItineraryEntity.class, tripItineraryId);
//        
//        // tripItinerary for tourist does not exist;
//        if (tripItinerary == null) {
//            // EXCEPTION
//            throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
//        }
//        else {
//            tripItinerary.setArrivalDate(null);
//            tripItinerary.setArrivalTime(null);
//            tripItinerary.setDepartureDate(null);
//            tripItinerary.setDepartureTime(null);
//            
//            em.persist(tripItinerary);
//            em.flush();
//        }
//    }
    
    public void updateTripItineraryAccomodationInfo(Long tripItineraryId, String accomodationPostalCode) throws ExistException {
        // find tripItinerary
        tripItinerary = em.find(TripItineraryEntity.class, tripItineraryId);
        
        // tripItinerary for tourist does not exist;
        if (tripItinerary == null) {
            // EXCEPTION
            throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        }
        else {
            tripItinerary.setAccomodationPostalCode(accomodationPostalCode);
            
            em.persist(tripItinerary);
            em.flush();
        }
    }
    
    public void updateTripItineraryOtherInfo(Long tripItineraryId, Integer noOfAdults, Integer noOfChildren) throws ExistException {
        // find tripItinerary
        tripItinerary = em.find(TripItineraryEntity.class, tripItineraryId);
        
        // tripItinerary for tourist does not exist;
        if (tripItinerary == null) {
            // EXCEPTION
            throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
        }
        else {
            tripItinerary.setNoOfAdults(noOfAdults);
            tripItinerary.setNoOfChildren(noOfChildren);
            
            em.persist(tripItinerary);
            em.flush();
        }
    }
    
    
//    
//    public void deleteTripItinerary(Long touristAccountId, Long tripItineraryId) throws ExistException {
//        // find account in persistence context
//        touristAccount = em.find(TouristAccountEntity.class, touristAccountId);
//        
//        // tourist account for tourist does not exist;
//        if (touristAccount == null) {
//            // EXCEPTION
//            throw new ExistException(systemMessages.getSystemMessage("TOURIST_ACCT_DOES_NOT_EXIST", selectedLocale));
//        }
//        else {
//            Collection<TripItineraryEntity> newTripItineraries = touristAccount.getTourist().getTripItineraries();
//            for (TripItineraryEntity t : newTripItineraries) {
//                if (t.getId() == tripItineraryId) {
//                    touristAccount.getTourist().getTripItineraries().remove(t);
//                }
//            }
//            
//            touristAccount.getTourist().setTripItineraries(newTripItineraries);
//            em.persist(touristAccount);
//            em.flush();
//        }
//    }
    /* DAILY ITINERARY */
//    public void updateDailyItinerary(Long touristAccountId, Long tripItineraryId, Long dailyItineraryId) throws ExistException {
//        // find account in persistence context
//        dailyItinerary = em.find(DailyItineraryEntity.class, dailyItineraryId);
//        
//        // trip itinerary for tourist does not exist;
//        if (tripItinerary == null) {
//            // EXCEPTION
//            throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
//        }
//        else {
//            tripItinerary.updateItineraryName(itineraryName);
//            
//            em.persist(tripItinerary);
//            em.flush();
//        }
//    }
//    public void deleteDailyItinerary(Long touristAccountId, Long tripItineraryId, Long dailyItineraryId) throws ExistException {
//        // find account in persistence context
//        touristAccount = em.find(TouristAccountEntity.class, touristAccountId);
//        
//        // tourist account for tourist does not exist;
//        if (touristAccount == null) {
//            // EXCEPTION
//            throw new ExistException(systemMessages.getSystemMessage("TOURIST_ACCT_DOES_NOT_EXIST", selectedLocale));
//        }
//        else {
//            tripItinerary = em.find(TripItineraryEntity.class, tripItineraryId);
//            
//            if (tripItinerary == null) {
//                // EXCEPTION
//                throw new ExistException(systemMessages.getSystemMessage("TRIP_ITINERARY_DOES_NOT_EXIST", selectedLocale));
//            }
//            else {
//                dailyItinerary = em.find(DailyItineraryEntity.class, dailyItineraryId);
//                
//                if (dailyItinerary == null) {
//                    // EXCEPTION
//                    throw new ExistException(systemMessages.getSystemMessage("DAILY_ITINERARY_DOES_NOT_EXIST", selectedLocale));
//                }
//                else {
//                    tripItinerary.
//                    em.persist(touristAccount);
//                    em.flush();
//                }
//            }
//        }
//    }
    @Remove
    public void remove() {
        System.out.println("ItinerarySessionBean:remove()");
    }
}
